﻿Imports System.Data

Partial Class Content_Administracion_Adm_controls_Cotitulares
    Inherits System.Web.UI.UserControl
    Dim IDataBase As New Clases.DataBase
    Dim ECConnString As String = ConfigurationManager.AppSettings("dbIP")
    Dim IControles As New Clases.controles
    Dim ctaid As New Integer
    Dim clid As New Integer
    Dim op As New Integer

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        clid = Request.QueryString("id")
        ctaid = Request.QueryString("ctaid")
        op = Request.QueryString("op")
        
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            If op = 1 Then
                btnSiguiente2.Visible = True
            Else
                btnSiguiente2.Visible = False
            End If

            cargalistCot(ctaid)

            fechaNacCot.Attributes.Add("onKeyPress", "return false;")

            Dim numcot As Integer = IDataBase.getSimpleData("SELECT count(cot_id) as numCot FROM vwCotitulares where cta_id=" & ctaid & "", "numCot")

            If numcot < 1 Then
                ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Todavía No Hay Cotitulares Registrados')", True)

            End If
            btnSiguiente2.Visible = True
        End If
    End Sub

    Protected Sub cargalistCot(ByVal cta_id As Integer)
        Dim cot = "select * from vwCotitulares where cta_id=" & cta_id & " "

        IControles.fillGridViewPaging(gv_Cotitulares, cot)

    End Sub


  

    Protected Sub btnRegresar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegresar.Click
        Response.Redirect("Clientes.aspx?id=" & clid & "&op=edit")
    End Sub

    Protected Sub btnInsertaCot_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsertaCot.Click

        Dim dmlInsertCot(), resultTransaction, InsertCotitulares As String

        InsertCotitulares = "insert into TCotitulares(cta_id,cot_nombre,cot_apellido_p,cot_apellido_m,cot_fecha_nac,cot_rfc,cot_curp,cot_calle,cot_colonia,cot_numero,pais_nombre,mun_nombre,edo_nombre,cot_cp,cot_telefono,cot_celular,stat_id,cot_titular)" & _
       " values ('" & ctaid & "','" & nomCot.Text & "','" & apCot.Text & "','" & amCot.Text & "','" & fechaNacCot.Text & "','" & rfcCot.Text & "','" & curpCot.Text & "','" & calleCli.Text & "','" & txt_colonia.Text & "','" & txt_numero.Text & "','" & txt_pais.Text & "','" & munCot.Text & "','" & edocot.Text & "'," & cpCot.Text & "," & telCot.Text & "," & celCot.Text & ",1,'" & Session("ActiveUserlogin") & "')"

        IDataBase.addStringToArray(dmlInsertCot, InsertCotitulares)

        resultTransaction = IDataBase.execute_commandsArray(dmlInsertCot, lb_error)

        If resultTransaction = "OK" Then

            ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Cotitular Registrado Exitosamente');window.location.href='Cotitulares.aspx?id=" & clid & "&ctaid=" & ctaid & "'", True)
        Else

            ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Error En Transacción : " & Replace(resultTransaction, "'", "") & " \n\n Consulta Al Administrador Del Sistema');", True)
        End If

    End Sub

    Protected Sub gv_Cotitulares_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_Cotitulares.RowCommand
        Dim cotid As Integer = e.CommandArgument

        If e.CommandName = "editarCot" Then
            clearFieldData()
            loadCotData(cotid)
        End If


        If e.CommandName = "eliminarCot" Then
            Dim dmlDeleteCot() As String
            Dim DeleteCot As String
            Dim resultTransaction As String

            DeleteCot = "delete  FROM TCotitulares where cot_id='" & cotid & "' "
            IDataBase.addStringToArray(dmlDeleteCot, DeleteCot)

            resultTransaction = IDataBase.execute_commandsArray(dmlDeleteCot, lb_error)

            If resultTransaction = "OK" Then
                ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Cotitular Eliminado Exitosamente');window.location.href='Cotitulares.aspx?id=" & clid & "&ctaid=" & ctaid & "'", True)
            Else
                ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Error En Transacción : " & Replace(resultTransaction, "'", "") & " \n\n Consulta Al Administrador Del Sistema');", True)
            End If
        End If



       


    End Sub

    Protected Sub clearFieldData()
        btnInsertaCot.Visible = True
        btnModificarCot.Visible = False

        nomCot.Text = ""
        apCot.Text = ""
        amCot.Text = ""
        fechaNacCot.Text = ""
        rfcCot.Text = ""
        curpCot.Text = ""
        calleCli.Text = ""
        txt_numero.Text = ""
        txt_colonia.Text = ""
        cpCot.Text = ""
        txt_pais.Text = ""
        edocot.Text = ""
        munCot.Text = ""
        telCot.Text = ""
        celCot.Text = ""
    End Sub

    Protected Sub loadCotData(ByVal cotID As Integer)
        btnInsertaCot.Visible = False
        btnModificarCot.Visible = True

        Dim datoscot As New DataTable

        Dim dmlGetCotData As String

        dmlGetCotData = "select Distinct Top 1 * from vwCotitulares where cot_id = '" & cotID & "' "
        IDataBase.createDataTable(datoscot, dmlGetCotData)

        If datoscot IsNot Nothing Then
            nomCot.Text = datoscot.Rows(0)("cot_nombre")
            apCot.Text = datoscot.Rows(0)("cot_apellido_p")
            amCot.Text = datoscot.Rows(0)("cot_apellido_m")
            fechaNacCot.Text = datoscot.Rows(0)("cot_fecha_nac")
            rfcCot.Text = datoscot.Rows(0)("cot_rfc")
            curpCot.Text = datoscot.Rows(0)("cot_curp")
            calleCli.Text = datoscot.Rows(0)("cot_calle")
            txt_numero.Text = datoscot.Rows(0)("cot_numero")
            txt_colonia.Text = datoscot.Rows(0)("cot_colonia")
            cpCot.Text = datoscot.Rows(0)("cot_cp")
            txt_pais.Text = datoscot.Rows(0)("pais_nombre")
            edocot.Text = datoscot.Rows(0)("edo_nombre")
            munCot.Text = datoscot.Rows(0)("mun_nombre")
            telCot.Text = datoscot.Rows(0)("cot_telefono")
            celCot.Text = datoscot.Rows(0)("cot_celular")

        End If

        Session("cotID") = cotID

    End Sub

    Protected Sub btnModificarCot_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnModificarCot.Click
        Dim dmlUpdatecot() As String
        Dim Updatecot As String
        Dim resultTransaction As String


        Updatecot = "UPDATE TCotitulares SET cot_nombre = '" & nomCot.Text & "',cot_apellido_p = '" & apCot.Text & "',cot_apellido_m =  '" & amCot.Text & "',cot_fecha_nac = " & fechaNacCot.Text & "" & _
                     ",cot_rfc = '" & rfcCot.Text & "',cot_curp = '" & curpCot.Text & "',cot_calle = '" & calleCli.Text & "',cot_numero = '" & txt_numero.Text & "'" & _
                     " ,cot_colonia = '" & txt_colonia.Text & "',pais_nombre = '" & txt_pais.Text & "',cot_cp = '" & cpCot.Text & "',mun_nombre = '" & munCot.Text & "',edo_nombre = '" & edocot.Text & "'" & _
                     " ,cot_telefono = '" & telCot.Text & "',cot_celular =  '" & celCot.Text & "' " & _
                     "WHERE cta_id =" & ctaid & ""
        IDataBase.addStringToArray(dmlUpdatecot, Updatecot)

        resultTransaction = IDataBase.execute_commandsArray(dmlUpdatecot, lb_error)

        If resultTransaction = "OK" Then
            ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Cotitular Modificado Exitosamente');window.location.href='Cotitulares.aspx?id=" & clid & "&ctaid=" & ctaid & "'", True)
        Else
            ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Error En Transacción : " & Replace(resultTransaction, "'", "") & " \n\n Consulta Al Administrador Del Sistema');", True)
        End If


    End Sub

    Protected Sub bntCancelarCot_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bntCancelarCot.Click
        clearFieldData()
    End Sub

    Protected Sub btnSiguiente2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSiguiente2.Click
        Dim numcot As Integer = IDataBase.getSimpleData("SELECT count(cot_id) as numCot FROM vwCotitulares where cta_id=" & ctaid & "", "numCot")

        If numcot < 1 Then
            ScriptManager.RegisterStartupScript(Page, Me.GetType(), "PopupScript", "alert('Todavía No Hay Cotitulares Registrados');window.location.href='Beneficiarios.aspx?id=" & clid & "&ctaid=" & ctaid & "&op=1'", True)
        Else
            Response.Redirect("Beneficiarios.aspx?id=" & clid & "&ctaid=" & ctaid & "&op=1")
        End If
    End Sub
End Class
